package com.ftg.learn.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.entity.PmsFeightTemplate;
import com.ftg.learn.mall.service.impl.PmsFeightTemplateServiceImpl;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 运费模版 前端控制器
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/mall/pmsFeightTemplate")
public class PmsFeightTemplateController {
    @Autowired
    PmsFeightTemplateServiceImpl service;

    /**
     * 查询全部运费模板
     * @return pageEntity
     */
    @GetMapping("/findFeightTemplate")
    public ResEntity findFeightTempalte(){
        return new ResEntity(0,"success",service.findAllFeightTempalte());
    }

    /**
     * 查询运费模板分页显示含模糊搜索
     * @param keyword
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/findListPage")
    public ResPageEntity findListPage(@RequestParam(value = "keyword", required = false) String keyword,
                                      @RequestParam int page, @RequestParam int limit){
        IPage<PmsFeightTemplate> ip = new Page<>(page,limit);
        if (!StringUtils.isBlank(keyword)){
            QueryWrapper<PmsFeightTemplate> query = new QueryWrapper<>();
            query.like("feight_template_name",keyword);
            service.page(ip,query);
            return new ResPageEntity(ip);
        }
        service.page(ip);
        return new ResPageEntity(ip);
    }

    @DeleteMapping("/deleteIds")
    public ResEntity deleteIds(@RequestParam List<Serializable> ids){
        if ( service.removeByIds(ids)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

//    @DeleteMapping("/deleteId/{id}")
//    public ResEntity deleteForId(@PathVariable("id") Long id){
//        if ( service.removeById(id)) {
//            return new ResEntity(0,"success");
//        }
//        return new ResEntity(-1,"failure");
//    }
}
